<template>
  <BasicModal @register="register" :title="title" @ok="handleSubmit">
    <BasicForm @register="registerForm" />
    <p style="color: red; margin-left: 20px"> 提示：如果需要【提前预约】首检,请填写预约时间。</p>
  </BasicModal>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'WorkCalendarModal',
  });
</script>
<script setup lang="ts">
  import { defineComponent, ref } from 'vue';
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { BasicForm, FormSchema, useForm } from '/@/components/Form';
  import { updateBsj } from '/@/api/m8/machine/task';
  import { dateUtil } from '/@/utils/dateUtil';
  const emit = defineEmits(['success', 'register']);
  const { showMessage } = useMessage();
  // import { DatePicker } from 'ant-design-vue';
  let record = ref<any>();
  const [register, { closeModal, setModalProps }] = useModalInner(async (e) => {
    // , { closeModal, setModalProps }
    value.value = '';
    record.value = e;
    title.value = '首检预约';
    await resetFields();
    let inputFormSchemas: FormSchema[] = [
      {
        label: '预约时间',
        field: 'updateDate',
        // defaultValue: new Date(),
        defaultValue: dateUtil(new Date()),
        component: 'DatePicker',
        componentProps: {
          format: 'YYYY-MM-DD HH:mm',
          showTime: { format: 'HH:mm' },
        },
        required: true,
      },
    ];
    setProps({
      labelWidth: 120,
      schemas: inputFormSchemas,
      baseColProps: { lg: 24, md: 24 },
    });
  });
  let title = ref<string>('');
  let value = ref<string>('');

  async function handleSubmit() {
    try {
      const data = await validate();
      setModalProps({ confirmLoading: true });
      const params: any = {
        machine: record.value.code,
        id: record.value.task.taskCode,
        bsj: 1,
        ...data,
      };
      const res = await updateBsj(params);
      showMessage(res.message);
      setTimeout(closeModal);
      emit('success', data);
    } catch (error: any) {
      if (error && error.errorFields) {
        showMessage('您填写的信息有误，请根据提示修正。');
      }
      console.log('error', error);
    } finally {
      setModalProps({ confirmLoading: false });
    }
  }
  const [registerForm, { resetFields, validate, setProps }] = useForm();
</script>
<style scoped>
  .ant-picker {
    width: 80%;
  }
</style>
